<?php
require_once(ROOT_ADMIN_MODELS.DS.'Models_User.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Group.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Base.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Quest.php');
require_once(ROOT_ADMIN_OBJECT.DS.'Objs_Quest.php');
require_once(ROOT_ADMIN_MODELS.DS.'Models_Log.php'); 
require_once(ROOT_LIBRARY_UTILITY.DS.'utility.php');
require_once(ROOT_ADMIN_FORMS.DS.'Forms_Quest.php');
require_once(ROOT_ADMIN_CONTROLLERS.DS.'BaseController.php');
class Admin_QuestController extends BaseController {
	
	public function preDispatch()
	{
		parent::preDispatch();	
		if(!$this->hasPrivilege())
			$this->_redirect ("/admin/error/permission");
	}
		
	public function indexAction()
	{
		try 
		{	
			$this->view ->headTitle("UIT-News Supporter");		
			$md = new Models_Quest();
			$this->view->arrquest = $md->listquest();
			echo $this->view->arrquest;
		}
		catch(Exception $ex)
		{
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		} 	
	}
	
	public function editAction()
	{
		$this->view ->headTitle("UIT-News Supporter");
		try 
		{
			$id=$this->_request->getParam('id');			
			$md = new Models_Quest();
			require_once(ROOT_ADMIN_MODELS.DS.'Models_News.php');
			$mdnews = new Models_News();
			$this->view->arrNews = $mdnews->listNews("","","1","","","");
			if($this->_request->isPost())
			{										
				$id = $this->_request->getParam("id");
				$qtitle = $this->_request->getParam('qtitle');
				$md->updateAnswer($id, $qtitle);
				Models_Log::insert($this->view->user->id, "act_update_quest","id=".$id);
				require_once(ROOT_ADMIN_MODELS.DS.'Models_Answer.php');
				$mdanswer = new Models_Answer();
				$mdanswer->delAnswerByqid($id);
				$acount = $this->_request->getParam("acount");
				$atitle  = $this->_request->getParam("atitle");
				require_once(ROOT_ADMIN_OBJECT.DS."Objs_Answer.php");
				$objanswer = new Obj_Answer();
				if($acount)
				{
					foreach($acount as $key=>$value)
					{
						if($atitle[$key] != "")
						{
							$objanswer->aid = "NULL";
							$objanswer->acount = $value;
							$objanswer->atitle = $atitle[$key];
							$objanswer->qid = $id;
	
							$mdanswer->_insert($objanswer);
						}
					}
				}
				Models_Log::insert($this->view->user->id, "act_update_answer","");
				require_once(ROOT_ADMIN_MODELS.DS.'Models_Quest_News.php');
				require_once(ROOT_ADMIN_OBJECT.DS."Objs_Quest_News.php");
				$mdQuestNews = new Models_Quest_News();
				$objquestnews = new Objs_Quest_News();
				$mdQuestNews->delquest_newsByqid($id);
				$news_id = $this->_request->getParam("news_id");
				if($news_id)
				{
					foreach ($news_id as $value)
					{
						if($value != "Chọn tin")
						{
							$objquestnews->id = "NULL";
							$objquestnews->news_id = $value;
							$objquestnews->q_id = $id;

							$mdQuestNews->_insert($objquestnews);
						}
					}
				}
				Models_Log::insert($this->view->user->id, "act_update_quest_news","");
			}
			$this->view->quest=$md->getquestByid($id);
		}
		catch(Exception $ex)
		{
			echo($ex->getMessage());
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
	public function newAction()
	{
		$this->view ->headTitle("UIT-News Supporter");
		try
		{
			$today = getdate();
			$this->view->date = $today['year']."-".$today['month']."-".$today['mday'];
			if($this->_request->isPost())
			{
				$qtitle = $this->_request->getParam('qtitle');
				
				$md = new Models_Quest();
				$id=$md->insert($qtitle);
				Models_Log::insert($this->view->user->id, "act_new_quest","");
				$this->_redirect("/admin/quest/edit/id/".$id);
			}
		}
		catch(Exception $ex)
		{
			echo($ex->getMessage());
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
	
	public function deleteAction()
	{
		try
		{
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNorender();
			$id=$this->_request->getParam('id');
			
			require_once(ROOT_ADMIN_MODELS.DS.'Models_Answer.php');
			$mdanswer = new Models_Answer();
			$mdanswer->delAnswerByqid($id);
			
			require_once(ROOT_ADMIN_MODELS.DS.'Models_Quest_News.php');
			$mdQuestNews = new Models_Quest_News();
			$mdQuestNews->delquest_newsByqid($id);
			
			$md = new Models_Quest();
			$md->_delete($id);
			echo 1;
		}
		catch(Exception $ex)
		{
			echo($ex->getMessage());
			$this->view->errMsg = $ex->getMessage();
			Utility::log($ex->getMessage(), $ex->getFile(), $ex->getLine());
		}
	}
}